//
//  NSString+Validation.m
//  base
//
//  Created by Demi on 14/04/2017.
//  Copyright © 2017 Azazie. All rights reserved.
//

#import "NSString+Validation.h"
#import "TXFire.h"

@implementation NSString (Validation)

- (BOOL)isValidEmail
{
    BOOL isEmailFormat = NO;
    if (TXRegexMatch(@"^\\w[\\w.+-]*@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$", self) && ![self containsString:@".."] && self.length <= 64) {
        
        NSArray *split = [self componentsSeparatedByString:@"@"];
        
        if (split.count == 2) {
            
            NSString *localPart = split[0];
            NSString *domainPart = split[1];
            
            isEmailFormat = !([[localPart substringFromIndex:localPart.length - 1] isEqualToString:@"."] ||
                            [[domainPart substringFromIndex:domainPart.length - 1] isEqualToString:@"."] ||
                            [[domainPart substringFromIndex:domainPart.length - 1] isEqualToString:@"-"] ||
                            [[domainPart substringToIndex:1] isEqualToString:@"-"]);
        }
    }
    return isEmailFormat;
}

- (BOOL)isValidPassword
{
    return self.length >= 5;
}

- (BOOL)isOnlyContainLetterAndNumber
{
    NSString *string = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (TXRegexMatch(@"^[a-zA-Z0-9]*$", self)) {
        return YES;
    }
    return NO;
}

- (BOOL)isContainSpecialCharacters
{
    if (!TXRegexMatch(@"^[\\u0000-\uffff]*$", self)) {
        return YES;
    }
    
    __block BOOL returnValue = NO;
    
    [self enumerateSubstringsInRange:NSMakeRange(0, [self length])
                             options:NSStringEnumerationByComposedCharacterSequences
                          usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
                              const unichar hs = [substring characterAtIndex:0];
                              if (0xd800 <= hs && hs <= 0xdbff) {
                                  if (substring.length > 1) {
                                      const unichar ls = [substring characterAtIndex:1];
                                      const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                      if (0x1d000 <= uc && uc <= 0x1f77f) {
                                          returnValue = YES;
                                      }
                                  }
                              } else {
                                  if (0x2100 <= hs && hs <= 0x27ff) {
                                      returnValue = YES;
                                  } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                      returnValue = YES;
                                  } else if (0x2934 <= hs && hs <= 0x2935) {
                                      returnValue = YES;
                                  } else if (0x3297 <= hs && hs <= 0x3299) {
                                      returnValue = YES;
                                  } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                      returnValue = YES;
                                  }
                              }
                              if (substring.length > 1) {
                                  const unichar ls = [substring characterAtIndex:1];
                                  if (ls == 0x20e3) {
                                      returnValue = YES;
                                  }
                              }
    }];
    return returnValue;
}

- (BOOL)isHexColorFormat {
    return TXRegexMatch(@"^#([0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$", self);
}

- (BOOL)isRGBColorFormat {
    NSString *normalInsets = @"([\\s]*(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)[\\s]*,){2}([\\s]*(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)[\\s]*){1}";
    NSString *alphaInsets = @"(,[\\s]*(0\\.\\d{1,2}|1|0)[\\s]*)?";
    NSString *rgbFormat = [NSString stringWithFormat:@"^[rR][gG][Bb][Aa]?[\(]%@%@[\)][\\s]*$", normalInsets, alphaInsets];
//    NSString *rgbFormat = [NSString stringWithFormat:@"^[rR][gG][Bb][Aa]?[\(]%@,?[\)][\\s]*$", normalInsets];
    return TXRegexMatch(rgbFormat, self);
}

@end
